<?php
ob_start();
session_start();
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<?php 
include('menu.php'); 
include("../connect/connect.php");
?>
<script language="javascript">
function ListSub(SelectValue)
{
	var result;
	var result2;
	<?php 
	$cQuery = mysql_query("SELECT category_id FROM category");
	while($cRow = mysql_fetch_array($cQuery)){
	?>
	if(SelectValue==<?php echo $cRow['category_id'];?>){
		<?php 
		$vQuery = mysql_query("SELECT category_head FROM category WHERE category_id = '".$cRow['category_id']."'");
		$vRow = mysql_fetch_array($vQuery);
		
		$pQuery = mysql_query("SELECT max(code_num) as code_num FROM product WHERE category_id = '".$cRow['category_id']."'");
		$pRow = mysql_fetch_array($pQuery);
		$code = $pRow['code_num']+1;
		$codeShow = sprintf("%04d",$code);
		?>
		result = "<?php echo $vRow['category_head'].$codeShow;?>";
		result2 = "<?php echo $codeShow;?>";
	}
	<?php } ?>

	if(SelectValue==""){
		result = "<font color=\"red\">รหัสสร้างอัตโนมัติหลังจากเลือกหมวดสินค้า</font>";
		result2 = "";
	}
	
	document.getElementById("lacode").innerHTML = result;
	document.getElementById("code").value = result;
	document.getElementById("code_num").value = result2;
}

function open_popup(){
	var name = document.getElementById('name');
	var category_id = document.getElementById('category_id');
	var cost = document.getElementById('cost');
	var price = document.getElementById('price');
	var rop = document.getElementById('rop');

	if(name.value == ""){
		alert('กรุณากรอกชื่อสินค้า');
		name.focus();
		return false;
	}

	if(category_id.value == ""){
		alert('กรุณาเลือกหมวดสินค้า');
		return false;
	}

	if(cost.value == ""){
		alert('กรุณากรอกต้นทุน');
		cost.focus();
		return false;
	}

	if(price.value == ""){
		alert('กรุณากรอกราคาขาย');
		price.focus();
		return false;
	}
	
	if(rop.value == ""){
		alert('กรุณากรอกจุดสั่งซื้อ');
		rop.focus();
		return false;
	}

	document.formadd.submit();
}
</script>


</head>
<body onload="document.formadd.name.focus();">
<?php

if($_GET["action"]==add) {

	if($_POST["code"]!="" && $_POST["code_num"]!="" && $_POST["name"]!="" && $_POST["cost"]!= "" && $_POST["price"]!="" && $_POST["category_id"]!="" && $_POST["rop"]!="")
		{
		
		$checkcode = "SELECT code FROM product WHERE code = '".$_POST["code"]."' AND category_id = '".$_POST['category_id']."'";
		$result = mysql_query($checkcode) or die ("'".checkcode."'");
		$row = mysql_num_rows($result);
		
		if($row>=1){
			$save="รหัสสินค้า ".$_POST["code"]." นี้มีอยู่แล้วกรุณาใช้รหัสอื่น";
			} else {
			$Query = "INSERT INTO product (code,code_num,name,cost,price,category_id,rop) VALUES ";
			$Query .="('".$_POST["code"]."','".$_POST["code_num"]."','".$_POST["name"]."','".$_POST["cost"]."','".$_POST["price"]."','".$_POST["category_id"]."','".$_POST["rop"]."' )";
			$objQuery = mysql_query($Query);

			if($objQuery) {
				$save="<script>alert('บันทึกข้อมูลเรียบร้อย');window.location='product_add.php'</script>";
				//header('Location: product_add.php');
				} else {
				echo "Error Save [".$Query."]";
				}
			}
		} else {
		$save="กรุณากรอกข้อมูลให้ครบ";
		}
}
?>
<form action="?action=add" name="formadd" method="post" id="formadd">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>เพิ่มสินค้า</h1>
    <div class="buttons"><a onclick="open_popup();" class="button"><span>บันทึก</span></a><a onclick="window.location='product_manage.php'" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form">
      <tr>
        <td>รหัสสินค้า : </td>
        <td><label id="lacode"><font color="red">รหัสสร้างอัตโนมัติหลังจากเลือกหมวดสินค้า</font></label><input type="hidden" name="code" id="code" value="" /><input type="hidden" name="code_num" id="code_num" value="" /></td>
      </tr>
      <tr>
        <td>ชื่อสินค้า : </td>
        <td><input type="text" name="name" value="" id="name" onKeyUp="IsScharacter(this.value,this)" onkeydown="if(event.keyCode==13){open_popup();}" /></td>
      </tr>
      <tr>
        <td>หมวดสินค้า : </td>
        <td><select name="category_id" id="category_id" onChange = "ListSub(this.value)" >
                <option value=""><-- Please Select --></option>
                <?php
			$Querycate = "SELECT * FROM category ORDER BY category_id ASC";
			$objQuerycate = mysql_query($Querycate);
			$strNum=0;
			while($row = mysql_fetch_array($objQuerycate))
			{
			$strNum++;
			?>
                <option value="<?php echo $row["category_id"];?>">
                <?php echo $strNum." - ".$row["category_name"];?>
                </option>
                <?php
			}
			?>
              </select></td>
      </tr>
      <tr>
        <td>ต้นทุน : </td>
        <td><input type="text" name="cost" id="cost" onKeyUp="IsNumeric(this.value,this)" onkeydown="if(event.keyCode==13){open_popup();}" /></td>
      </tr>
      <tr>
        <td>ราคาขาย : </td>
        <td><input type="text" name="price" id="price" onKeyUp="IsNumeric(this.value,this)" onkeydown="if(event.keyCode==13){open_popup();}" /></td>
      </tr>
      <tr>
        <td>จุดสั่งซื้อ : </td>
        <td><input type="text" name="rop" id="rop" value="0" onKeyUp="IsNumeric(this.value,this)" onkeydown="if(event.keyCode==13){open_popup();}" /></td>
      </tr>
    </table>
	<?php echo "<br><font color=\"red\"><center>$save</center></font>"; ?>
  </div>
</div>
</div>
</form>
<?php mysql_close(); ?>
</body>
</html>
